<?$this->load->view("/inc/header_inc")?>
<?$this->load->view("/inc/nav_inc")?>
<div class="content">
    <div class="header">
        <ul class="breadcrumb">
            <li><a href="/home">首页</a> </li>
            <li><a href="/admin/user">用户管理</a></li>
            <li class="active">添加编辑用户</li>
        </ul>
    </div>
    <div class="main-content">
        <div class="row">
            <div class="col-md-8">
                <div id="myTabContent" class="tab-content">
                    <div class="tab-pane active in" id="home">
                        <p class="text-info">说明：带*的为必填项。</p>
                        <form id="useraddform" class="form-horizontal" name="useraddform" method="post" action="/admin/user/saveOrUpdate">
                            <?
                            if(isset($user)){
                                $addflag=false;
                                ?>
                                <input type="hidden" name="id" id="id" value="<?=$user['id']?>" >
                            <?}else{
                                $user=array(
                                    "username"=>"",
                                    "name"=>"",
                                    "password"=>"sys123",
                                    "type"=>"0",
                                );
                                $province = "";
                                $placename = "";
                                $addflag=true;
                                $pid = [0];
                            }?>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">用户名</label>
                                <div class="col-sm-8">
                                    <input type="text"  id="username" name="username" value="<?=$user['username']?>" class="form-control" >
                                </div>
                                <div class="col-sm-2 "><p class="text-warning">(*)</p></div>
                            </div>


                            <div class="form-group">
                                <label class="col-sm-2 control-label">用户类型</label>
                                <div class="col-sm-8">
                                    <select name="user_type" id="user_type" class="form-control">
                                        <option value="0" <?=$user['type']==0?"selected":""?>>普通用户</option>
                                        <option value="1" <?=$user['type']==1?"selected":""?>>普通管理员</option>
                                        <option value="2" <?=$user['type']==2?"selected":""?>>系统管理员</option>
                                    </select>
                                </div>
                                <div class="col-sm-2"><p class="text-warning">(*)</p></div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-2 control-label">密码</label>
                                <div class="col-sm-8">
                                    <input type="text"  id="username" name="password" value="<?=$user['password']?>" class="form-control" >
                                </div>
                                <div class="col-sm-2"><p class="text-warning">(*)</p></div>
                            </div>

                            <?
                                for($i=0; $i < count($pid); $i++){
                            ?>
                             <div class="form-group">
                                <label class="col-sm-2 control-label">所负责监测区域<span class="num"><?= $i+1 ?></span></label>
                                <div class="col-sm-8">
                                    <select name="place[]" id="placename" class="form-control">
                                     <?
                                    foreach($places as $p) {
                                        ?>
                                        <option value="<?= $p['id']?>" <?=$p['id']==$pid[$i]?"selected":""?> data-province="<?= $p['province']?>"><?= $p['name']?></option>
                                     <?
                                    }
                                    ?>
                                    </select>
                                </div>
                                <div class="col-sm-2"><p class="text-warning">(*)</p></div>
                            </div>
                            <?
                            }
                            ?>

                            <div class="form-group">
                                <div class="btn-toolbar list-toolbar">           
                                    <input type="submit" id="useraddbtn"  class="btn btn-success pull-right" value="保存"></input>
                                   <?
                                    if($addflag == true){
                                        ?>
                                        <input type="button" id="addPlaceBtn"  class="btn btn-success pull-right" value="增加监测区域"></input>
                                    <?}
                                    ?>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal small fade" id="successModal" tabindex="-1" role="dialog" aria-labelledby="successModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="myModalLabel">成功</h3>
            </div>
            <div class="modal-body">
                <p class="error-text"><i class="fa fa-success modal-icon" ></i><span id="modal-success-content"></span></p>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true" id="continue-btn">继续操作</button>
                <button class="btn btn-danger" data-dismiss="modal" id="finish-btn">结束操作</button>
            </div>
        </div>
    </div>
</div>
<div class="modal small fade" id="errorModal" tabindex="-1" role="dialog" aria-labelledby="errorModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="errorModalLabel">错误</h3>
            </div>
            <div class="modal-body">
                <p class="error-text"><i class="fa fa-warning modal-icon" ></i><span id="modal-error-content"></span></p>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">继续操作</button>
                <button class="btn btn-danger" data-dismiss="modal">结束操作</button>
            </div>
        </div>
    </div>
</div>
<script src="/static/javascripts/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="/static/javascripts/bootstrap.min.js"></script>
<script src="/static/javascripts/jquery.validate.min.js"></script>
<script src="/static/javascripts/bootstrap-editable.min.js"></script>
     <script type="text/javascript">
        $(function(){
            $("form").validate({
                rules: {
                    username: "required",
                    password: "required"
                },
                messages: {
                    username: "请输入用户名",
                    password: "请输入密码"
                },
                submitHandler:function (form) {
                    $.ajax({
                        type: $(form).attr('method'),
                        url: $(form).attr('action'),
                        data: $(form).serialize(),
                        dataType : 'json'
                    }).done(function (data) {
                        $("#login-info").removeClass("success");
                        $("#login-info").removeClass("error");
                        if (data.code == 1) {
                                showSuccess(data.msg);
                            } else {
                                showError(data.msg);
                            }
                        });
                      return false; // required to block normal submit since you used ajax
                 }
            });
            function showSuccess(content)
                {
                    $('#successModal').modal('toggle');
                    $('#successModal').modal('show');
                    $("#modal-success-content").text(content);
                }

            function showError(content)
            {
                $('#errorModal').modal('toggle');
                $('#errorModal').modal('show');
                $("#modal-error-content").text(content);
            }

            $("#continue-btn").click(function(){
                window.location.reload();
            });
            $("#finish-btn").click(function(){
                window.location.href="/admin/user/";
            });

            $("#addPlaceBtn").click(function(){
                var target = $('.form-group').eq(-2);
                target.clone(true).insertBefore('.form-group:last');
                var oldNum = target.find('.num').html();
                $('.form-group').eq(-2).find('.num').html(1+oldNum++);
            })
        });
    </script>
</body>
</html>
